<template>
  <div class="column-detail-page w-75 mx-auto">
    <div
      class="column-info row mb-4 border-bottom pb-4 align-items-center"
      v-if="column"
    >
      <div class="col-3 text-center">
        <img
          :src="column.avatar.url"
          :alt="column.title"
          class="rounded-circle border w-100"
        />
      </div>
      <div class="col-9">
        <h4>{{ column.title }}</h4>
        <p class="text-muted">{{ column.description }}</p>
      </div>
    </div>
    <post-list :list="list"></post-list>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { GlobalDataProps } from '../store'
import PostList from '../components/PostList.vue'

export default defineComponent({
  components: {
    PostList
  },
  setup() {
    const route = useRoute()
    const store = useStore<GlobalDataProps>()
    // 加号效果：将 string 类型的 id 转换为 number 类型
    const currentId = route.params.id
    onMounted(() => {
      store.dispatch('fetchColumn', currentId)
      store.dispatch('fetchPosts', currentId)
    })
    // 找到专栏中和当前专栏相同的页面：找的是元素
    // const column = testData.find(c => c.id === currentId)
    // 修改为 vuex 获取数据
    // const column = computed(() => store.state.columns.find(c => c.id === currentId))
    // 修改为 通过 getters 获取数据
    const column = computed(() => store.getters.getColumnById(currentId))
    // 找到那个 id 对应的具体内容：找的是数组
    // const list = testPosts.filter(post => post.columnId === currentId)
    // const list = computed(() => store.state.posts.filter(post => post.columnId === currentId))
    const list = computed(() => store.getters.getPostByCid(currentId))
    return {
      column,
      list
    }
  }
})
</script>

<style>
</style>
